/**
 * 
 */
package com.java.test.user.mapper;

import com.java.test.user.dto.BankAccountInfo;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Administrator
 *
 */
@Component
public class BankAccountMapper {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectBankAccountCount(BankAccountInfo info) {
		String sql = "select count(*) from member_play_account a "
				+ "left JOIN member_bank_account b on a.account=b.game_account where 1=1 ";
		if (info.getDrawingType() != 0) {
			sql += " and b.drawing_type='" + info.getDrawingType() + "'";
		}
		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += "and a.account like '%" + info.getGameAccount() + "%'";
		}

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i == null ? null : i;

	}

	public List<BankAccountInfo> selectBankAccountList(BankAccountInfo info, PageInfo<BankAccountInfo> page) {
		String sql = "select a.account as game_account,b.id,b.drawing_type,b.bank_account,"
				+ "b.bank_account_name,b.tel_number from member_play_account a "
				+ "left JOIN member_bank_account b on a.account=b.game_account where 1=1 ";
		if (info.getDrawingType() != 0) {
			sql += " and b.drawing_type='" + info.getDrawingType() + "'";
		}
		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += "and a.account like '%" + info.getGameAccount() + "%'";
		}

		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += "limit ?,?";
		List<BankAccountInfo> list = jdbcTemplate.query(sql, new BankAccountInfo(), start, limit);
		return list;
	}

	public int insertBankAccount(BankAccountInfo info) {
		String sql = "insert into member_bank_account"
				+ "(game_account,drawing_type,bank_account,bank_account_name,tel_number)" + "values(?,?,?,?,?)";
		int i = jdbcTemplate.update(sql, info.getGameAccount(), info.getDrawingType(), info.getBankAccount(),
				info.getBankAccountName(), info.getTelNumber());
		return i;
	}

	public int delBankAccount(BankAccountInfo info) {
		String sql = "delete from member_bank_account where id=?";
		int i = jdbcTemplate.update(sql, info.getId());
		return i;
	}

	public int updateBankAccount(BankAccountInfo info) {
		String sql = "update member_bank_account set drawing_type=?,"
				+ "bank_account=?,bank_account_name=?,tel_number=? where id=?";
		int i = jdbcTemplate.update(sql, info.getDrawingType(), info.getBankAccount(), info.getBankAccountName(),
				info.getTelNumber(), info.getId());
		return i;
	}

	public BankAccountInfo selectbackAcountInfo(BankAccountInfo info) {
		String sql = "select * from member_bank_account where id=?";
		BankAccountInfo rinfo = null;
		try {
			rinfo = jdbcTemplate.queryForObject(sql, new BankAccountInfo(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return rinfo;
	}

	public List<BankAccountInfo> selectBankAccountListPush(BankAccountInfo info) {
		String sql = "select a.account as game_account,b.id,b.drawing_type,b.bank_account,"
				+ "b.bank_account_name,b.tel_number from member_play_account a "
				+ "left JOIN member_bank_account b on a.account=b.game_account where 1=1 ";
		if (info.getGameAccount() != null && !"".equals(info.getGameAccount())) {
			sql += "and a.account like '%" + info.getGameAccount() + "%'";
		}

		List<BankAccountInfo> list = jdbcTemplate.query(sql, new BankAccountInfo());
		return list;
	}

}
